package com.kcsj.controller.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.kcsj.domain.user.Customer;
import com.kcsj.domain.user.User;
import com.kcsj.service.user.CustomerService;
import com.kcsj.util.Condition;
import com.kcsj.util.ControllerHelper;
import com.kcsj.util.JSONUtil;
import com.kcsj.util.MessageHelper;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

/**
 * @author 胡庆阳
 * 29th Nov. 2020
 */
@Slf4j
@WebServlet("/customer")
public class CustomerController extends HttpServlet {

    //添加客户
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //根据request对象，获得代表参数的JSON字串
        String json = JSONUtil.getJSON(request);
        try {
            JSONObject jsonObject = JSONObject.parseObject(json);
            //获取actor转为customer对象
            Customer customer = jsonObject.getObject("actor", Customer.class);
            //请求转为user对象
            User user = JSON.parseObject(json,User.class);
            user.setActor(customer);
            //添加客户
            CustomerService.getInstance().addUserOfCustomer(user);
            JSONObject message = MessageHelper.addMessage("添加成功");
            response.getWriter().println(message);;
        }catch (SQLException e){
            JSONObject message = MessageHelper.dateBaseError();
            e.printStackTrace();
            //响应message到前端
            response.getWriter().println(message);
        }catch(Exception e){
            JSONObject message = MessageHelper.netWordError();
            e.printStackTrace();
            //响应message到前端
            response.getWriter().println(message);
        }
    }
    //修改客户信息
    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String json = JSONUtil.getJSON(request);
        try {
            JSONObject jsonObject = JSONObject.parseObject(json);
            Customer customer = jsonObject.getObject("actor", Customer.class);
            User user = JSON.parseObject(json,User.class);
            user.setActor(customer);
            CustomerService.getInstance().updateInfo(user);
            log.info("user{}修改成功",user.getId());
            JSONObject message = MessageHelper.addMessage("修改成功");
            response.getWriter().println(message);
        }catch (SQLException e){
            log.error(e.getMessage());
            JSONObject message = MessageHelper.addMessage("数据库操作异常");
            response.getWriter().println(message);
        }catch(Exception e){
            JSONObject message = MessageHelper.netWordError();
            e.printStackTrace();
            //响应message到前端
            response.getWriter().println(message);
        }
    }
    //删除客户
    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String user_id = request.getParameter("id");
        try {
            //到数据库表中删除对应的学位
            int id = Integer.parseInt(user_id);
            CustomerService.getInstance().delete(id);
            JSONObject message = MessageHelper.addMessage("删除成功");
            log.info("user{}删除成功",id);
            response.getWriter().println(message);
        }catch (SQLException e){
            JSONObject message = MessageHelper.dateBaseError();
            e.printStackTrace();
            //响应message到前端
            response.getWriter().println(message);
        }catch(Exception e){
            JSONObject message = MessageHelper.netWordError();
            e.printStackTrace();
            //响应message到前端
            response.getWriter().println(message);
        }
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取前端请求的信息
        String id = request.getParameter("id");
        String condition = request.getParameter("condition");
        try {
            //如果id不等于空根据id查询
            if(id!=null){
                this.responseUser(Integer.parseInt(id), response);
            //如果condition不等于空根据条件查询
            }else if(condition!=null){
                JSONObject jsonObject = JSONObject.parseObject(condition);
                List<Condition> conditionList = ControllerHelper.getConditions(jsonObject);
                this.responseUsers(response,conditionList);
            }else {
                this.responseUsers(response,null);
            }
        }catch (SQLException e){
            log.error(e.getMessage());
            JSONObject message = MessageHelper.dateBaseError();
            response.getWriter().println(message);
        }catch(Exception e){
            JSONObject message = MessageHelper.netWordError();
            e.printStackTrace();
            //响应message到前端
            response.getWriter().println(message);
        }
    }

    private void responseUser(int id, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        //根据id查找客户
        User user = CustomerService.getInstance().find(id);
        String json = JSON.toJSONString(user);
        JSONObject data = MessageHelper.addData(json);
        //响应department_json到前端
        response.getWriter().println(data);
    }

    //按查询条件响应客户对象
    private void responseUsers(HttpServletResponse response, List<Condition> conditionList)
            throws ServletException, IOException, SQLException {
        //获得所有客户
        Collection<User> users = CustomerService.getInstance().findAll(conditionList);
        String users_json = JSON.toJSONString(users, SerializerFeature.DisableCircularReferenceDetect);
        JSONObject data = MessageHelper.addData(users_json);
        //响应department_json到前端
        response.getWriter().println(data);
    }
}
